package uk.co.q3c.deplan.server.util.rules;

import java.io.Serializable;

import javax.persistence.Id;

import uk.co.q3c.deplan.client.domain.AbstractDomainObject;
import uk.co.q3c.deplan.client.domain.DomainKind;
import uk.co.q3c.deplan.client.domain.DomainObject;
import uk.co.q3c.deplan.client.domain.resource.PersistenceReferenceException;
import uk.co.q3c.deplan.server.rcp.layout.WidgetType;

import com.googlecode.objectify.Key;

/**
 * Options for the layout and format for a specific property within an {@link AutoEditor} form. Contained within {@link ClassLayoutOptions},
 * {@link #max} and {@link #min} fields are not always used, but are there to set things such as field width and height - exact use varies
 * depending on the widget type
 * 
 * @author DSowerby 16 May 2009
 * 
 */
public class PropertyLayoutOptions extends AbstractDomainObject implements Serializable {

	private static final long serialVersionUID = 1L;
	@Id
	private Long keyId;
	private WidgetType widgetType = WidgetType.TEXT;
	/**
	 * Not sure I need this now
	 */
	private Integer widgetQualifier = 0;
	private Integer min = 0;
	private Integer max = 100;
	private final Integer pageIncrement = 10;

	public PropertyLayoutOptions() {
		super();
	}

	@Override
	protected void beforeLoad() {
		super.beforeLoad();
	}

	public WidgetType getWidgetType() {

		return widgetType;
	}

	public void setWidgetType(WidgetType widgetType) {

		this.widgetType = widgetType;
	}

	public int getMin() {

		return min;
	}

	public void setMin(int min) {

		Integer oldValue = this.min;
		this.min = new Integer(min);
		firePropertyChange("min", oldValue, this.min);
	}

	public int getMax() {

		return max;
	}

	public void setMax(int max) {

		Integer oldValue = this.max;
		this.max = new Integer(max);
		firePropertyChange("max", oldValue, this.max);
	}

	public int getWidgetQualifier() {

		return widgetQualifier;
	}

	public void setWidgetQualifier(int widgetQualifier) {
		Integer oldValue = this.widgetQualifier;
		this.widgetQualifier = new Integer(widgetQualifier);
		firePropertyChange("widgetQualifier", oldValue, this.widgetQualifier);
	}

	/**
	 * Returns true only if otherObject is an instance of {@link PropertyLayoutOptions} and has the same field values
	 * 
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object otherObject) {
		if (!(otherObject instanceof PropertyLayoutOptions)) {
			return false;
		}
		PropertyLayoutOptions other = (PropertyLayoutOptions) otherObject;
		if (this.getMax() != other.getMax()) {
			return false;
		}
		if (this.getMin() != other.getMin()) {
			return false;
		}
		if (this.getWidgetQualifier() != other.getWidgetQualifier()) {
			return false;
		}

		if (this.widgetType != null) {
			if (!this.widgetType.equals(other.getWidgetType())) {
				return false;
			}
		} else {
			return other.widgetType == null;
		}

		return true;
	}

	/**
	 * returns the pageIncrement property value of if that is null, a default of 10% of the range (max - min)
	 * 
	 * @return
	 */
	public int getPageIncrement() {
		if (pageIncrement == null) {
			return (max - min) / 10;
		}
		return pageIncrement;
	}

	@Override
	public DomainKind getKind() {
		return DomainKind.General;
	}

	public Long getKeyId() {
		return keyId;
	}

	@Override
	public Key<? extends DomainObject> getKey() throws PersistenceReferenceException {
		throw new RuntimeException("Not yet implemented");
	}

	@Override
	public void setKeyId(Long keyId) {
		throw new RuntimeException("Not yet implemented");
	}
}
